package com.sbhotel.chotellauncher.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.sbhotel.chotellauncher.MyApplication;
import com.sbhotel.chotellauncher.R;
import com.sbhotel.chotellauncher.activity.WifiConnectActivity;
import com.sbhotel.chotellauncher.constant.Constants;
import com.sbhotel.chotellauncher.util.SPUtil;
import com.sbhotel.chotellauncher.util.Utils;

public class NetWorkErrorDialog extends Dialog implements View.OnFocusChangeListener, View.OnClickListener {

    Context mContext;
    View connectWifi;

    public NetWorkErrorDialog(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_net_error);

        initView();
        //initData();

        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        //设置window背景，默认的背景会有Padding值，不能全屏。当然不一定要是透明，你可以设置其他背景，替换默认的背景即可。
        //getWindow().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(mContext, R.color.black_trans_90)));
        //一定要在setContentView之后调用，否则无效
        //getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        //getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().getDecorView().setBackground(ContextCompat.getDrawable(getContext(), R.mipmap.background));
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                PixelFormat.TRANSLUCENT);

        getWindow().setAttributes(params);
    }

    public void initView() {
        connectWifi = findViewById(R.id.tv_connect_wifi);
        connectWifi.setOnClickListener(this);
        connectWifi.requestFocus();
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        switch (view.getId()) {
            case R.id.tv_connect_wifi:
                Log.d(Constants.LOG_TAG, "connect wifi get focus");
                break;
            default:
                break;
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_connect_wifi:
                Log.d(Constants.LOG_TAG, "connect wifi click");
                getContext().startActivity(new Intent(getContext(), WifiConnectActivity.class));
                handler.sendEmptyMessageDelayed(1, 1000);
                //dismiss();
                break;
            default:
                break;
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:  //dismiss dialog
                    dismiss();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

}
